/** EasyWeb spa v3.1.7 date:2020-02-08 License By http://easyweb.vip */
layui.define(function (exports) {

    var layRouter = {

        index: "/",
        lash: null,
        routers: {},

        init: function (option) {
            layRouter.index = layRouter.routerInfo(option.index).path.join("/");
            if (option.pop && typeof option.pop === "function") {
                layRouter.pop = option.pop
            }
            if (option.notFound && typeof option.notFound === "function") {
                layRouter.notFound = option.notFound
            }
            push();
            window.onhashchange = function () {
                push()
            };
            return this
        },

        /**
         * 注册路由
         * @param url 路由链接
         * @param callback 点击链接回调函数
         * @returns {layRouter}
         */
        reg: function (url, callback) {
            if (url) {
                if (!callback) {
                    callback = function () {
                    }
                }
                if (url instanceof Array) {
                    for (var urlKey in url) {
                        this.reg.apply(this, [url[urlKey], callback]) // 递归注册
                    }
                } else {
                    if (typeof url === "string") {
                        url = layRouter.routerInfo(url).path.join("/"); // 规范路由路径
                        if (typeof callback === "function") {
                            layRouter.routers[url] = callback
                        } else {
                            if (typeof callback === "string" && layRouter[callback]) {
                                layRouter.routers[url] = layRouter.routers[callback]
                            }
                        }
                    }
                }
            }
            return this
        },

        /**
         * 获取路由对象信息
         * @param url 可以是"#/system/user"、"/system/user"、"system/user"类型参数
         * @returns {{path: *[], search: {}, hash: string}}
         */
        routerInfo: function (url) {
            url || (url = location.hash);
            var regularUrl = url.replace(/^#+/g, "").replace(/\/+/g, "/");
            if (regularUrl.indexOf("/") !== 0) {
                regularUrl = "/" + regularUrl
            }
            return layui.router("#" + regularUrl) // 返回{path:[],search:{},hash:''}
        },

        refresh: function (url) {
            push(url, true)
        },

        // 跳转到指定的url
        go: function (url) {
            location.hash = "#" + layRouter.routerInfo(url).href
        }
    };

    // 路由跳转
    function push(url, isRefresh) {
        var router = layRouter.routerInfo(url);
        layRouter.lash = router.href;
        var routePath = router.path.join("/");
        if (!routePath || routePath === "/") {
            routePath = layRouter.index;
            router = layRouter.routerInfo(layRouter.index);
        }
        layRouter.pop && layRouter.pop.call(this, router);
        if (layRouter.routers[routePath]) {
            router.refresh = isRefresh;
            layRouter.routers[routePath].call(this, router);
        } else {
            if (layRouter.notFound) {
                layRouter.notFound.call(this, router);
            }
        }
    }

    exports("layRouter", layRouter)
});